{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "5c2ef9e3-78dc-4c5f-812a-293c0f84378b",
   "metadata": {},
   "source": [
    "【1】"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ebf7020c-84d4-4877-bdfb-0c1f156b69f2",
   "metadata": {},
   "source": [
    "金字塔图"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f5cd61d5-6a80-4029-a8ba-c11221580949",
   "metadata": {},
   "source": [
    "人口金字塔图：人口金字塔图是一种表示性别与年龄构成的塔状条形统计图。水平条代表每一年龄组男性和女性的数值成比例，各个年龄性别组相加构成了总人口反映某一地区过去和现在的人口统计趋势"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "e2278f8f-46bb-4d64-bce0-e6a4a552d0cf",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'D:\\\\jupyter\\\\金字塔图.html'"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import pandas as pd\n",
    "from pyecharts.charts import Bar,Grid\n",
    "from pyecharts.globals import JsCode,ThemeType\n",
    "from pyecharts import options as opts\n",
    "\n",
    "data = pd.read_excel('./data/age.xlsx')\n",
    "data_pair = data.values.tolist()\n",
    "# 定义JavaScript代码作为格式化器\n",
    "jscode = \"function(x) { return Number(-x.data);}\"\n",
    "# 定义itemstyle\n",
    "itemstyle1 = {\n",
    "    \"normal\":{\n",
    "                \"shadowOffsetY\":2, # 考虑堆叠y轴偏置可以调小点\n",
    "                \"color\":'#49a9dc',\n",
    "                \"shadowOffsetX\":2,\n",
    "                \"barBorderRadius\":[2,2,2,2],\n",
    "                \"opacity\":1,\n",
    "                \"shadowBlur\":2\n",
    "                    }\n",
    "}\n",
    "itemstyle2 = {\n",
    "    \"normal\":{\n",
    "                \"shadowOffsetY\":2,\n",
    "                \"color\":'#fded72',\n",
    "                \"shadowOffsetX\":2,\n",
    "                \"barBorderRadius\":[2,2,2,2],\n",
    "                \"opacity\":1,\n",
    "                \"shadowBlur\":2\n",
    "                    }\n",
    "}\n",
    "\n",
    "# 创建第一个条形图（男性）\n",
    "bar1 = (\n",
    "    Bar(init_opts=opts.InitOpts(theme=ThemeType.DARK))\n",
    "    .add_xaxis([i[0] for i in data_pair])\n",
    "    .add_yaxis('男性',[i[1] for i in data_pair],itemstyle_opts=itemstyle1,\n",
    "              label_opts=opts.LabelOpts(position='right',font_weight=500))\n",
    "    .reversal_axis() # 反转x轴和y轴\n",
    "    .set_global_opts(\n",
    "        legend_opts=opts.LegendOpts(pos_left='53%',pos_top='5%'),\n",
    "        xaxis_opts=opts.AxisOpts(\n",
    "            is_show=True,\n",
    "            axisline_opts=opts.LineStyleOpts(is_show=False),\n",
    "            axistick_opts=opts.AxisTickOpts(is_show=False),\n",
    "            axislabel_opts=opts.LabelOpts(font_weight=500,margin=12)\n",
    "        ),\n",
    "        yaxis_opts=opts.AxisOpts(\n",
    "            is_inverse=True,\n",
    "            is_show=True,\n",
    "            axisline_opts=opts.LineStyleOpts(is_show=False),\n",
    "            axistick_opts=opts.AxisTickOpts(is_show=False),\n",
    "            axislabel_opts=opts.LabelOpts(font_weight=500,margin=-4),\n",
    "            position='right'\n",
    "        ),\n",
    "        tooltip_opts=opts.TooltipOpts(\n",
    "            trigger='axis',\n",
    "            axis_pointer_type='cross',\n",
    "            background_color='rgba(245,245,245,0.9)',\n",
    "            border_width=2,\n",
    "            border_color='cyan',\n",
    "            textstyle_opts=opts.TextStyleOpts(color='#000',font_weight=500)\n",
    "        ),\n",
    "        axispointer_opts=opts.AxisPointerOpts(\n",
    "            is_show=True,\n",
    "            link=[{\"yAxisIndex\":\"all\"}],\n",
    "            label=opts.LabelOpts(background_color=\"#fff\",font_weight=500,color='black')\n",
    "        )\n",
    "    )\n",
    ")\n",
    "# 创建第二个条形图（女性）\n",
    "bar = (\n",
    "    Bar()\n",
    "    .add_xaxis([i[0] for i in data_pair])\n",
    "    .add_yaxis('女性',[i[2] for i in data_pair],itemstyle_opts=itemstyle2,\n",
    "              label_opts=opts.LabelOpts(position='left',font_weight=500))\n",
    "    .reversal_axis()\n",
    "    .set_global_opts(\n",
    "        title_opts=opts.TitleOpts(title='中国人口性别分布金字塔图',pos_left='center'),\n",
    "        legend_opts=opts.LegendOpts(pos_left='45%',pos_top='5%'),\n",
    "        yaxis_opts=opts.AxisOpts(\n",
    "            is_show=True,\n",
    "            is_inverse=True,\n",
    "            axisline_opts=opts.LineStyleOpts(is_show=False),\n",
    "            axistick_opts=opts.AxisTickOpts(is_show=False),\n",
    "            axislabel_opts=opts.LabelOpts(font_weight=500)\n",
    "        ),\n",
    "        xaxis_opts=opts.AxisOpts(\n",
    "            is_show=True,\n",
    "            is_inverse=True,\n",
    "            axistick_opts=opts.AxisTickOpts(is_show=False),\n",
    "            axisline_opts=opts.LineStyleOpts(is_show=False),\n",
    "            axislabel_opts=opts.LabelOpts(font_weight=500,margin=12)\n",
    "        )\n",
    "    )\n",
    ")\n",
    "\n",
    "# 使用grid布局\n",
    "grid = (\n",
    "    Grid(init_opts=opts.InitOpts(theme=ThemeType.DARK))\n",
    "    .add(bar1,grid_opts=opts.GridOpts(pos_left=\"50%\",pos_top=\"10%\",pos_bottom=\"5%\",pos_right=\"5%\"))\n",
    "    .add(bar,grid_opts=opts.GridOpts(pos_left=\"7%\",pos_top=\"10%\",pos_bottom=\"5%\",pos_right=\"50%\"))\n",
    ")\n",
    "\n",
    "grid.render('金字塔图.html')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b0587798-77ee-40eb-8acc-1320c170cc95",
   "metadata": {},
   "source": [
    "【2】"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "16f08b29-9ef4-4cb3-b9ab-6aceadac1f39",
   "metadata": {},
   "source": [
    "词云图"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "da057849-b539-468b-97ed-8fecbbff5b30",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'D:\\\\jupyter\\\\基础词云图.html'"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from pyecharts import options as opts\n",
    "from pyecharts.charts import WordCloud # 导入词云图\n",
    "\n",
    "data = [\n",
    "(\"生活资源\", \"999\"),\n",
    "(\"供热管理\", \"888\"),\n",
    "(\"供气质量\", \"777\"),\n",
    "(\"生活用水管理\", \"688\"),\n",
    "(\"一次供水问题\", \"588\"),\n",
    "(\"交通运输\", \"516\"),\n",
    "(\"城市交通\", \"515\"),\n",
    "(\"环境保护\", \"483\"),\n",
    "(\"房地产管理\", \"462\"),\n",
    "(\"城乡建设\", \"449\"),\n",
    "(\"社会保障与福利\", \"429\"),\n",
    "(\"社会保障\", \"407\"),\n",
    "(\"文体与教育管理\", \"406\"),\n",
    "(\"公共安全\", \"406\"),\n",
    "(\"公交运输管理\", \"386\"),\n",
    "(\"出租车运营管理\", \"385\"),\n",
    "(\"供热管理\", \"375\"),\n",
    "(\"市容环卫\", \"355\"),\n",
    "(\"自然资源管理\", \"355\"),\n",
    "(\"粉尘污染\", \"335\"),\n",
    "(\"噪声污染\", \"324\"),\n",
    "(\"土地资源管理\", \"304\"),\n",
    "(\"物业服务与管理\", \"304\"),\n",
    "(\"医疗卫生\", \"284\"),\n",
    "(\"粉煤灰污染\", \"284\"),\n",
    "(\"占道\", \"284\"),\n",
    "(\"供热发展\", \"254\"),\n",
    "(\"农村土地规划管理\", \"254\"),\n",
    "(\"生活噪音\", \"253\"),\n",
    "(\"供热单位影响\", \"253\"),\n",
    "(\"城市供电\", \"223\"),\n",
    "(\"房屋质量与安全\", \"223\"),\n",
    "(\"大气污染\", \"223\"),\n",
    "(\"房屋安全\", \"223\"),\n",
    "(\"文化活动\", \"223\"),\n",
    "(\"拆迁管理\", \"223\"),\n",
    "(\"公共设施\", \"223\"),\n",
    "(\"供气质量\", \"223\"),\n",
    "(\"供电管理\", \"223\"),\n",
    "(\"燃气管理\", \"152\"),\n",
    "(\"教育管理\", \"152\"),\n",
    "(\"医疗纠纷\", \"152\"),\n",
    "(\"执法监督\", \"152\"),\n",
    "(\"设备安全\", \"152\"),\n",
    "(\"政务建设\", \"152\"),\n",
    "(\"县区、开发区\", \"152\"),\n",
    "(\"宏观经济\", \"152\"),\n",
    "(\"教育管理\", \"112\"),\n",
    "(\"社会保障\", \"112\"),\n",
    "(\"生活用水管理\", \"112\"),\n",
    "(\"物业服务与管理\", \"112\"),\n",
    "(\"分类列表\", \"112\"),\n",
    "(\"农业生产\", \"112\"),\n",
    "(\"二次供水问题\", \"112\"),\n",
    "(\"城市公共设施\", \"92\"),\n",
    "(\"拆迁政策咨询\", \"92\"),\n",
    "(\"物业服务\", \"92\"),\n",
    "(\"物业管理\", \"92\"),\n",
    "(\"社会保障保险管理\", \"92\"),\n",
    "(\"低保管理\", \"92\"),\n",
    "(\"文娱市场管理\", \"72\"),\n",
    "(\"城市交通秩序管理\", \"72\"),\n",
    "(\"执法争议\", \"72\"),\n",
    "(\"商业烟尘污染\", \"72\"),\n",
    "(\"占道堆放\", \"71\"),\n",
    "(\"地上设施\", \"71\"),\n",
    "(\"水质\", \"71\"),\n",
    "(\"无水\", \"71\"),\n",
    "(\"供热单位影响\", \"71\"),\n",
    "(\"人行道管理\", \"71\"),\n",
    "(\"主网原因\", \"71\"),\n",
    "(\"集中供热\", \"71\"),\n",
    "(\"客运管理\", \"71\"),\n",
    "(\"国有公交（大巴）管理\", \"71\"),\n",
    "(\"工业粉尘污染\", \"71\"),\n",
    "(\"治安案件\", \"71\"),\n",
    "(\"压力容器安全\", \"71\"),\n",
    "(\"身份证管理\", \"71\"),\n",
    "(\"群众健身\", \"41\"),\n",
    "(\"工业排放污染\", \"41\"),\n",
    "(\"破坏森林资源\", \"41\"),\n",
    "(\"市场收费\", \"41\"),\n",
    "(\"生产资金\", \"41\"),\n",
    "(\"生产噪声\", \"41\"),\n",
    "(\"农村低保\", \"41\"),\n",
    "(\"劳动争议\", \"41\"),\n",
    "(\"劳动合同争议\", \"41\"),\n",
    "(\"劳动报酬与福利\", \"41\"),\n",
    "(\"医疗事故\", \"21\"),\n",
    "(\"停供\", \"21\"),\n",
    "(\"基础教育\", \"21\"),\n",
    "(\"职业教育\", \"21\"),\n",
    "(\"物业资质管理\", \"21\"),\n",
    "(\"拆迁补偿\", \"21\"),\n",
    "(\"设施维护\", \"21\"),\n",
    "(\"市场外溢\", \"11\"),\n",
    "(\"占道经营\", \"11\"),\n",
    "(\"树木管理\", \"11\"),\n",
    "(\"农村基础设施\", \"11\"),\n",
    "(\"无水\", \"11\"),\n",
    "(\"供气质量\", \"11\"),\n",
    "(\"停气\", \"11\"),\n",
    "(\"市政府工作部门（含部门管理机构、直属单位）\", \"11\"),\n",
    "(\"燃气管理\", \"11\"),\n",
    "(\"市容环卫\", \"11\"),\n",
    "(\"新闻传媒\", \"11\"),\n",
    "(\"人才招聘\", \"11\"),\n",
    "(\"市场环境\", \"11\"),\n",
    "(\"行政事业收费\", \"11\"),\n",
    "(\"食品安全与卫生\", \"11\"),\n",
    "(\"城市交通\", \"11\"),\n",
    "(\"房地产开发\", \"11\"),\n",
    "(\"房屋配套问题\", \"11\"),\n",
    "(\"物业服务\", \"11\"),\n",
    "(\"物业管理\", \"11\"),\n",
    "(\"占道\", \"11\"),\n",
    "(\"园林绿化\", \"11\"),\n",
    "(\"户籍管理及身份证\", \"11\"),\n",
    "(\"公交运输管理\", \"11\"),\n",
    "(\"公路（水路）交通\", \"11\"),\n",
    "(\"房屋与图纸不符\", \"11\"),\n",
    "(\"有线电视\", \"11\"),\n",
    "(\"社会治安\", \"11\"),\n",
    "(\"林业资源\", \"11\"),\n",
    "(\"其他行政事业收费\", \"11\"),\n",
    "(\"经营性收费\", \"11\"),\n",
    "(\"食品安全与卫生\", \"11\"),\n",
    "(\"体育活动\", \"11\"),\n",
    "(\"有线电视安装及调试维护\", \"11\"),\n",
    "(\"低保管理\", \"11\"),\n",
    "(\"劳动争议\", \"11\"),\n",
    "(\"社会福利及事务\", \"11\"),\n",
    "(\"一次供水问题\", \"11\"),\n",
    "]\n",
    "\n",
    "(\n",
    "    WordCloud()\n",
    "    .add(series_name='热点分析',\n",
    "        data_pair=data,\n",
    "        word_size_range=[6,66])\n",
    "    .set_global_opts(\n",
    "        title_opts=opts.TitleOpts(\n",
    "            title='热点分析',\n",
    "            title_textstyle_opts=opts.TextStyleOpts(font_size=23)\n",
    "        ),\n",
    "        tooltip_opts=opts.TooltipOpts(is_show=True),\n",
    "    )\n",
    "    .render('基础词云图.html')\n",
    ")"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
